# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

"""
Version 1.0
- 记录训练损失和权重/偏差值的历史记录
- 可以显示绘图
"""
class TrainingHistory_1_0(object):
    def __init__(self):
        self.iteration = []
        self.loss_history = []
        self.w_history = []
        self.b_history = []

    def AddLossHistory(self, iteration, loss, w, b):
        self.iteration.append(iteration)
        self.loss_history.append(loss)
        self.w_history.append(w)
        self.b_history.append(b)

    def ShowLossHistory(self, params, x_min=None, x_max=None, y_min=None, y_max=None, show=True, subplot=111):
        plt.plot(self.iteration, self.loss_history)
        title = params.toString_title()
        plt.title(title)
        plt.xlabel("iteration")
        plt.ylabel("loss")
        if x_min != None and y_min != None:
            plt.figure(figsize=(6, 4))
            plt.subplot(subplot)
            plt.axis([x_min, x_max, y_min, y_max])
        if show is True:
            plt.show()
        return title

    def GetLast(self):
        count = len(self.loss_history)
        return self.loss_history[count-1], self.w_history[count-1], self.b_history[count-1]
# end class
